package com.ytzl.itrip.search.service.impl;

import com.ytzl.itrip.beans.model.vo.Hotel;
import com.ytzl.itrip.beans.model.vo.SearchHotCityVo;
import com.ytzl.itrip.beans.model.vo.SearchHotelVo;
import com.ytzl.itrip.search.dao.BaseQuery;
import com.ytzl.itrip.search.service.SearchHotelService;
import com.ytzl.itrip.utils.common.EmptyUtils;
import com.ytzl.itrip.utils.common.Page;
import com.ytzl.itrip.utils.common.PropertiesUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by asus on 2018/7/23.
 */
@Service("searchHotelService")
public class SearchHotelServiceImpl implements SearchHotelService{

    private String baseUrl = PropertiesUtils
            .get("public.properties","baseUrl");
    private BaseQuery<Hotel> hotelSoleQuery = new BaseQuery<Hotel>(baseUrl);

    @Override
    public Page<Hotel> searchHotelListByHotCity(SearchHotelVo searchHotelVo) throws Exception {
        SolrQuery solrQuery = new SolrQuery("*:*");
        StringBuffer tempquery = new StringBuffer();
        // 拼接全文检索  q查询   目的地   关键字查询
        if (EmptyUtils.isNotEmpty(searchHotelVo.getDestination())){
            tempquery.append(" destination:"+searchHotelVo.getDestination());
        }
        if (EmptyUtils.isNotEmpty(searchHotelVo.getKeywords())){
            tempquery.append(" and keywords:"+searchHotelVo.getKeywords());
        }
        solrQuery.setQuery(tempquery.toString());
        //酒店级别
        if (EmptyUtils.isNotEmpty(searchHotelVo.getHotelLevel())){
            solrQuery.addFilterQuery("hotelLevel:"+searchHotelVo.getHotelLevel());
        }
        if (EmptyUtils.isNotEmpty(searchHotelVo.getFeatureIds())){
            String[] split = searchHotelVo.getFeatureIds().split(",");
            StringBuffer fqQuery = new StringBuffer();
            for (int i=0;i<split.length;i++){
                String trueId = split[i];
                if (i == 0){
                    //第一次循环
                    fqQuery.append(" featureIds:*,"+trueId+",*");
                }else{
                    fqQuery.append("or featureIds:*,"+trueId+",*");
                }
            }
            solrQuery.addFilterQuery(fqQuery.toString());
        }

        //最高价
        if (EmptyUtils.isNotEmpty(searchHotelVo.getMaxPrice())){
            solrQuery.addFilterQuery(" maxPrice:["+searchHotelVo.getMaxPrice()+"]");
        }
        //最低价
        if (EmptyUtils.isNotEmpty(searchHotelVo.getMinPrice())){
            solrQuery.addFilterQuery(" maxPrice:["+searchHotelVo.getMinPrice()+"]");
        }
        //商圈id
        if (EmptyUtils.isNotEmpty(searchHotelVo.getTradeAreaIds())){
            String[] split = searchHotelVo.getTradeAreaIds().split(",");
            StringBuffer fqQuery = new StringBuffer();
            for (int i=0;i<split.length;i++){
                String areaId = split[i];
                if (i==0){
                    fqQuery.append(" tradeAreaIds:*,"+areaId+",*");
                }else{
                    fqQuery.append("or tradeAreaIds:*,"+areaId+",*");
                }
            }
            solrQuery.addFilterQuery(fqQuery.toString());
        }
        //字段降序
        if (EmptyUtils.isNotEmpty(searchHotelVo.getDescSort())){
            solrQuery.addSort(searchHotelVo.getDescSort(), SolrQuery.ORDER.desc);
        }
        //字段升序
        if (EmptyUtils.isNotEmpty(searchHotelVo.getAscSort())){
            solrQuery.addSort(searchHotelVo.getAscSort(), SolrQuery.ORDER.asc);
        }
            return hotelSoleQuery.queryPage(solrQuery,searchHotelVo.getPageNo(),
                searchHotelVo.getPageSize(),Hotel.class);
    }

    @Override
    public List<Hotel> searchHoteByHotCity(SearchHotCityVo searchHotCityVo) throws Exception {
        //创建solr查询，指定q参数为查询全部
        SolrQuery solrQuery = new SolrQuery("*:*");
        //添加过滤查询
        if (EmptyUtils.isNotEmpty(searchHotCityVo.getCityId())){
            solrQuery.addFilterQuery("cityId:"+searchHotCityVo.getCityId());
        }
        return hotelSoleQuery.queryList(solrQuery,searchHotCityVo.getCount(),Hotel.class);
    }
}

